package v1

import (
	"github.com/gin-gonic/gin"
	"guns-go/model/request"
	"guns-go/model/response"
	"guns-go/service"
)

// @Tags Org
// @Summary 机构树
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysOrgParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysOrg/tree [get]
func TreeOrg(c *gin.Context) {

	var S request.SysOrgParam
	_ = c.ShouldBindQuery(&S)

	_, isAdmin := service.IsSuperAdmin(c)

	err, info := service.OrgInfoTree(isAdmin, S)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(info, c)
}

// @Tags Org
// @Summary 机构列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysOrgParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysOrg/list [get]
func SysOrgList(c *gin.Context) {

	var S request.SysOrgParam
	_ = c.ShouldBindQuery(&S)

	_, isAdmin := service.IsSuperAdmin(c)

	err, info := service.SysOrgList(isAdmin, &S)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithData(info, c)
}

// @Tags Org
// @Summary 机构分页查询
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysOrgParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysOrg/page [get]
func OrgPage(c *gin.Context) {

	var U request.SysOrgParam
	_ = c.ShouldBindQuery(&U)

	err, OrgPageResult := service.OrgPage(&U, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(OrgPageResult, c)
}

// @Tags Org
// @Summary 机构新增
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysOrgParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysOrg/add [post]
func OrgAdd(c *gin.Context) {

	var U request.SysOrgParam
	_ = c.ShouldBindJSON(&U)

	err := service.OrgAdd(&U, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Org
// @Summary 机构修改
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysOrgParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysOrg/edit [post]
func OrgEdit(c *gin.Context) {

	var U request.SysOrgParam
	_ = c.ShouldBindJSON(&U)

	err := service.OrgEdit(&U, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// @Tags Org
// @Summary 机构删除
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysOrgParam true "请求参数"
// @Success 200 {string} string "{"success":true,"data":{},"message":"请求成功","code":200}"
// @Router /sysOrg/delete [post]
func OrgDelete(c *gin.Context) {

	var U request.SysOrgParam
	_ = c.ShouldBindJSON(&U)

	err := service.OrgDelete(&U, c)

	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}
